// pages/login2/login2.js
const app = getApp(), core = require('../../utils/core'), WeValidator = require('../../utils/we-validator'),
chatIM = require('../../utils/websocket.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav:0,
    wx_account: null,
    countdown: 0,
	mobile: '',
	isLogin:true
  },
  tab:function(e){
    this.setData({
      nav:e.currentTarget.dataset.index
    })
  },
  inputMobile: function (event) {
    this.setData({mobile: event.detail.value});
  },
  sendCaptcha: function() {
    let page = this;
    if(page.data.countdown > 0) {
      return;
    }
    let mobile = this.data.mobile;
    if(mobile.length!==11) {
      wx.showToast({title: '请输入正确的手机号', icon:'none'});
      return;
    }
    core.post({url: '/xilujob.sms/send', data: {mobile:mobile,event:'mobilelogin'}, success(ret, response) {
      page.setData({countdown: 60});
      let timer = setInterval(function() {
        let tick = page.data.countdown - 1;
        page.setData({countdown: tick});
        if(tick<=0) {
          clearInterval(timer);
        }
      },1000);
    }});
  },
  mobileLogin: function (event) {
    let data = event.detail.value;
    let validator = new WeValidator({
      rules: {
        mobile: {required:true, mobile:true},
        captcha: {required:true, length:4},
      },
      messages: {
        mobile: {required:'请输入手机号',mobile:'请输入正确的手机号'},
        captcha: {required: '请输入验证码', length: '请输入正确的验证码'},
      }
    });
    if(!validator.checkData(data)) {
      return;
    }
    core.post({url:'/xilujob.user/mobilelogin',data:data,success:(ret, response) => {
      this._loginSuccess(ret.data.userinfo);
    }});
  },
  emailLogin: function (event) {
    let page = this;
    let data = event.detail.value;
    let validator = new WeValidator({
      rules: {
        account: {required:true},
        password: {required:true, rangelength:[3,20]}
      },
      messages: {
        account: {required:'请输入用户名或邮箱'},
        password: {required: '请输入密码', rangelength: '密码最少3位，最多20位'}
      }
    });
    if(!validator.checkData(data)) {
      return;
    }
    core.post({url:'/xilujob.user/login',data:data,success(ret, response) {
      page._loginSuccess(ret.data.userinfo);
    }});
  },
  _loginSuccess(userinfo) {
    app.setUserinfo(userinfo);
    // wx.reLaunch({url:'/pages/indexpage/indexpage'});
    chatIM.connectSocket();
    if(userinfo.group_id==1 || userinfo.group_model.audit_status==1) {
      if(getCurrentPages().length===1) {
        wx.switchTab({url:'/pages/indexpage/indexpage'});
      }else {
        wx.navigateBack({delta:2});
      }
    }else {
      wx.reLaunch({url:'/pages/enterprise_audit/enterprise_audit'});
    }
    app.eventBus.emit(app.Events.UserLogin, userinfo);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let page = this;
    page.setData({
		wx_account: app.getWxAccount(),
		isLogin: app.getUserinfo()?true:false
	});
    app.eventBus.on(app.Events.WxAccountUpdate, page.eventOnWxAccountUpdate, page);
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    app.eventBus.off(app.Events.UserLogin, this.eventOnUserLogin);
  },

  eventOnWxAccountUpdate: function (wxAccount) {
    this.setData({wx_account: wxAccount});
  }


});
